package com.gdth.bank.activity;

import java.io.Serializable;
import java.util.List;

import com.gdth.bank.R;
import com.gdth.bank.adapter.ImageBucketAdapter;
import com.gdth.bank.model.ImageBucket;
import com.gdth.bank.utils.AlbumHelper;
import com.gdth.bank.utils.AppUtil;
import com.gdth.core.annotation.view.ViewInject;
import com.gdth.core.widget.BaseActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.GridView;

/**
 * 相册选择器
 * @author arvin
 *
 */
public class TestPicActivity extends BaseActivity {

	public static final String EXTRA_IMAGE_LIST = "imagelist";
	public static final String EXTRA_IMAGE_CLASS = "className";
	@ViewInject(id=R.id.header_label_title) TextView txt_header_title;
	@ViewInject(id=R.id.header_btn_left, click="finishClick") TextView txt_header_left;
    @ViewInject(id=R.id.header_btn_right) TextView txt_header_right;
	@ViewInject(id=R.id.pic_gridview, itemClick="onItemClick") GridView gridView;
	List<ImageBucket> dataList;
	/** 自定义的适配器 **/
	ImageBucketAdapter adapter;
	AlbumHelper helper;
	public static Bitmap bimap;
	private String className;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_image_bucket);

		helper = AlbumHelper.getHelper();
		helper.init(getApplicationContext());

		initData();
		initView();
	}

	/**
	 * 初始化数据
	 */
	private void initData() {
		dataList = helper.getImagesBucketList(false);
		bimap = BitmapFactory.decodeResource(getResources(),
				R.drawable.icon_addpic_unfocused);
	}

	/**
	 * 初始化view视图
	 */
	private void initView() {
		Intent intent = this.getIntent();
		className = intent.getStringExtra(EXTRA_IMAGE_CLASS);
		txt_header_right.setVisibility(View.GONE);
		txt_header_title.setText("相册");
		adapter = new ImageBucketAdapter(TestPicActivity.this, dataList);
		gridView.setAdapter(adapter);
	}
	
	/**
	 * gridview点击事件
	 * @param parent
	 * @param view
	 * @param position
	 * @param id
	 */
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		/**
		 * 根据position参数，可以获得跟GridView的子View相绑定的实体类，然后根据它的isSelected状态，
		 * 来判断是否显示选中效果。 至于选中效果的规则，下面适配器的代码中会有说明
		 * 通知适配器，绑定的数据发生了改变，应当刷新视图
		 */
		Intent intent = new Intent(TestPicActivity.this, ImageGridActivity.class);
		intent.putExtra(EXTRA_IMAGE_LIST,
				(Serializable) dataList.get(position).imageList);
		intent.putExtra(EXTRA_IMAGE_CLASS, className);
		
		this.startActivityForResult(intent, AppUtil.activity_pic_requestcode);
	}
	
	@Override  
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == AppUtil.activity_pic_requestcode && resultCode == AppUtil.activity_pic_resultcode){
			setResult(AppUtil.activity_picture_resultcode, null);
			finish();
		}
	}
}
